package com.itheima.controller;

import com.itheima.entity.Dept;
import com.itheima.entity.Result;
import com.itheima.service.DeptService;
import com.itheima.service.impl.DeptServiceImpl;
import org.apache.commons.io.IOUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {
//    @RequestMapping(value = "/depts" ,method = RequestMethod.GET)
//    private DeptService deptService = new DeptServiceImpl();
    @Autowired
    private DeptService deptService;
    /**
     * 查询部门列表
     */
//    @GetMapping("/depts")
    @GetMapping
    public Result list2() throws IOException {
        List<Dept> deptList = deptService.queryDeptList();
        //2. 响应数据
        return Result.success(deptList);
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
//    @DeleteMapping("/depts")
    @DeleteMapping
    public Result deleteById(@RequestParam("id") Integer id) {
        deptService.deleteById(id);
        return Result.success();
    }

    /**
     * 新增 部门
     * @param dept
     * @return
     */
//    @PostMapping("/depts")
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 根据id查询部门
     * @param id
     * @return
     */
//    @GetMapping("/depts/{id}")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        Dept dept = deptService.findById(id);
        return Result.success(dept);
    }

    /**
     * 修改部门
     * @param dept
     * @return
     */
//    @PutMapping("/depts")
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        System.out.println("修改部门数据" + dept);
        deptService.update(dept);
        return Result.success();
    }

}
